﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace Common.ExtendedMethod
{
   public static class StringEx
    {
       /// <summary>
       /// 正则替换
       /// </summary>
       /// <param name="src"></param>
       /// <param name="options"></param>
       /// <param name="replacement"></param>
       /// <returns></returns>
       public static string ReplaceEx(this string src,string options,string replacement)
       {
           if (string.IsNullOrEmpty(src))
           {
               return src;
           }
           Regex regex = new Regex(options);
           regex.Replace(src,replacement);
           return regex.Replace(src, replacement);
       }
       /// <summary>
       /// 将字符串转为整形转换失败返回-1
       /// </summary>
       /// <param name="src"></param>
       /// <returns>转换后的数据类型</returns>
       public static int ToIntEx(this string src)
       {
           if (string.IsNullOrEmpty(src))
           {
               return -1;
           }
           int result = 0;
           if (int.TryParse(src,out result))
           {
               return result;
           }
           return -1;
       }
       /// <summary>
       /// 将字符串转为decimal转换失败返回-1
       /// </summary>
       /// <param name="src"></param>
       /// <returns>转换后的数据类型</returns>
       public static decimal ToDecimalEx(this string src)
       {
           if (string.IsNullOrEmpty(src))
           {
               return -1;
           }
           decimal result = 0;
           if (decimal.TryParse(src, out result))
           {
               return result;
           }
           return -1;
       }
    }
}
